package com.xdap.common.util;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;

/**
 * @auth lishushu
 * @2023/11/23 11:58
 * @desc {读取在开发过程中可使用的配置文件}
 **/

@Configuration
public class FeishuPluginPropertiesUtil {
    private static Logger logger = LoggerFactory.getLogger(FeishuPluginPropertiesUtil.class);

    private static final String ENVIRONMENT_FILE_NAME = "application-environment.properties";
    private static final String FILE_NAME = "application.properties";
    private static final String FILE_NAME_COM = "application-common-";
    private static Properties props;
    private static String environmentName;
    @Value("application-${spring.application.name}")
    private static String fileName;

    static {
        props = new Properties();

        //  获取当前环境信息
        try {
            props.load(new InputStreamReader(
                    FeishuPluginPropertiesUtil.class.getClassLoader().getResourceAsStream(ENVIRONMENT_FILE_NAME), "UTF-8"));
            environmentName = props.getProperty("this.evironment"); // 环境名称
            logger.info("\n\n\n读取到的配置文件环境为为{}\n\n\n", environmentName);
        } catch (Exception e) {
            logger.error("配置this.evironment文件读取异常{}", e);
        }

        // 假设在获取不到 environmentName 的时候，设置 environmentName为dev环境，即是开发环境的数据
        if (StringUtils.isEmpty(environmentName)) {
            environmentName = "dev";
        }


        // 获取当前是本地，还是在服务器上运行
        try {
            props.load(new InputStreamReader(
                    FeishuPluginPropertiesUtil.class.getClassLoader().getResourceAsStream(FILE_NAME), "UTF-8"));
            String activeName = props.getProperty("spring.profiles.active"); // 环境名称

            // -----本地启动和环境启动适配
            // 因为可能是本地启动，如果是本地启动，就直接读取本地启动的环境信息--如果不是本地启动，则自动读取application.properties的信息
            if (!StringUtils.isEmpty(activeName)) {
                props.load(new InputStreamReader(
                        FeishuPluginPropertiesUtil.class.getClassLoader().getResourceAsStream("application-" + activeName +
                                ".properties"), "UTF-8"));
            }

            // 确定需要读取的配置文件的名称
            fileName = "application-" + props.getProperty("spring.application.name") + "-" + environmentName; // 应用名称

            logger.info("\n\n\n读取到的配置文件为{}\n\n\n", fileName + "-" + environmentName);
        } catch (Exception e) {
            logger.error("读取文件spring.application.name读取异常{}", e);
        }


        // 读取文件
        try {
            String fileDefault = fileName + ".properties";
            InputStream resourceAsStream = FeishuPluginPropertiesUtil.class.getClassLoader()
                    .getResourceAsStream(fileDefault);
            props.load(new InputStreamReader(resourceAsStream, "UTF-8"));
            logger.info("\n\n\n读取到的配置文件为{}\n\n\n", fileName);
        } catch (Exception e) {
            logger.info("读取私有化配置文件失败，转而读取公共配置文件");

            fileName = FILE_NAME_COM + environmentName + ".properties";
            logger.info("\n\n\n读取到的配置文件为{}\n\n\n", fileName);
            // 如果读取私有化的配置文件无得情况下，读取common配置文件
            InputStream resourceAsStream = FeishuPluginPropertiesUtil.class.getClassLoader()
                    .getResourceAsStream(fileName);
            try {
                props.load(new InputStreamReader(resourceAsStream, "UTF-8"));
            } catch (Exception ioException) {
                logger.error("\n\n\n\n\n\n服务缺少必要的启动配置，如有参数的使用，会导致接口异常，请上传后再启动项目");
                e.printStackTrace();
            }

        }
    }

    /**
     * @USER liShuShu
     * @TIME 2023/2/14 0014 16:46
     * @DESC 获取公共配置文件
     **/
    public static String getProperty(String key) {
        String value = props.getProperty(key.trim());
        if (StringUtils.isEmpty(value)) {
            return null;
        }
        return value.trim();
    }


    public static String getEnvironmentName() {
        return environmentName;
    }

}